<!doctype html>
<html>
    <head>
        <title>Filters</title>
    </head>
    <body>
        <?php
            /*
             * filter_var() - Filters a single variable with a specified filter
             */
             $a = "1a";
             if(filter_var($a,FILTER_VALIDATE_INT))
             {
                 echo $a . " is a valid integer<br/>";
             }
             else
             {
                 echo $a . " is not a valid integer<br/>";
             }
             
             /*
              * Options must be put in an associative array with the name "options"
              */
             $var=300;

             $int_options = array(
             "options"=>array
              (
              "min_range"=>0,
              "max_range"=>256
              )
             );

            if(!filter_var($var, FILTER_VALIDATE_INT, $int_options))
              {
                echo("Integer is not valid<br/>");
              }
            else
              {
                 echo("Integer is valid<br/>");
              }
              
            /*
             * Validate user input
             */  
            if(isset($_GET["submit"]) && !empty($_GET["submit"]))
            {
                /*
                 * Check if an "email" input variable of the "GET" type exist
                 */
                if(!filter_has_var(INPUT_GET, "email"))
                  {
                  echo("Input type does not exist");
                  }
                else
                  {
                  if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
                    {
                    echo "E-Mail is not valid<br/>";
                    }
                  else
                    {
                    echo "E-Mail is valid<br/>";
                    /*
                     * Sanitizing email
                     */
                    echo filter_input(INPUT_GET,"email",FILTER_SANITIZE_EMAIL);
                    }
                  }
                  
                  
               /*
               * Filtering multiple inputs with filter_input_array();
               */
               $filters = array(
                   "name" => array(
                       "filter" => FILTER_SANITIZE_STRING
                   ),
                   "age" => array(
                       "filter" => FILTER_VALIDATE_INT,
                       "options" => array(
                           "min_range" => 18,
                           "max_range" => 99
                       )
                   ),
                   "email" => array(
                       "filter" =>FILTER_VALIDATE_EMAIL
                   )
               );
               $result = filter_input_array(INPUT_GET,$filters);
               if(!$result["age"])
               {
                   echo "Age must be between 18 and 99 years";
               }
               if(!$result["email"])
               {
                   echo "Email is invalid";
               }
               if(!$result["name"])
               {
                   echo "Username is invalid";
               }
            }
              
            
            
        ?>
        <form method="get" action="">
            <input type="text" name="email" placeholder="E-mail" />
            <input type="text" name="name" placeholder="Name" />
            <input type="text" name="age" placeholder="Age" />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>
